//
//  GYMainBottomToolView.m
//  LockManagement
//
//  Created by apple on 2018/7/21.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYMainBottomToolView.h"
#import "GYButton.h"
#import "GYBleOperationManager.h"

@interface GYMainBottomToolView()

/** 按钮标题数组 */
@property (nonatomic, strong) NSArray *btnTitles;

/** 按钮图片数据 */
@property (nonatomic, strong) NSArray *btnImages;

/** 背景lable */
@property (nonatomic, strong) UILabel *bgLable;
@end

@implementation GYMainBottomToolView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //初始化
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI {
    self.backgroundColor = BackGroundColor;
    //1.添加子控件
    CGFloat btnW = ScreenWidth/4;
    for (int i=0; i < self.btnImages.count; i++) {
        [self creatGYButton:CGRectMake(i*btnW, 60, btnW, 49) title:self.btnTitles[i] image:self.btnImages[i] tag:i+1];
    }
    
    //1.增加一个背景lable
    UILabel *bgLable = [[UILabel alloc] initWithFrame:CGRectMake(0, self.height-2, btnW, 2)];
    bgLable.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgLable];
    self.bgLable = bgLable;
    
    //2.添加lbale空间
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon"]];
    imageView.frame = CGRectMake(self.width-125, 0, 125, 60);
    [self addSubview:imageView];
    
    //3.添加按钮
    [self creatBtuWithFrame:CGRectMake(self.width-60, 5, 50, 50) BackGroundImage:@"lanyaguanbi" Tag:101];
    [self creatBtuWithFrame:CGRectMake(self.width-115, 5, 50, 50) BackGroundImage:@"me" Tag:102];
    
    //4.检测设备连接block的回到
    [GYBleOperationManager shareOperationManager].blueManagerConnectDeviceStateCllBack = ^(deviceConnectType connectState) {
        UIButton *btn = (UIButton *)[self viewWithTag:101];
        if (connectState == deviceConntTypeSuccess) {
            [btn setBackgroundImage:[UIImage imageNamed:@"lanya"] forState:UIControlStateNormal];
        }else {
            [btn setBackgroundImage:[UIImage imageNamed:@"lanyaguanbi"] forState:UIControlStateNormal];
        }
    };
}

- (void)btuClick:(GYButton *)sender {
    //1.改变lable的位置
    CGFloat btnW = ScreenWidth/4;
    self.bgLable.frame = CGRectMake((sender.tag-1)*btnW, self.height-2, btnW, 2);
    
    //回调
    self.bottomToolViewClickCallBack(sender.tag);
}

#pragma mark -- 封装方法

- (void)creatBtuWithFrame:(CGRect)frame BackGroundImage:(NSString *)imageName Tag:(NSInteger)tag {
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    btn.tag = tag;
    [btn addTarget:self action:@selector(btuClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:btn];
}

/**
 创建图片文字按钮
 
 @param title <#title description#>
 @param imageName <#imageName description#>
 */
- (void)creatGYButton:(CGRect)frame title:(NSString *)title image:(NSString *)imageName tag:(NSInteger)tag {
    
    GYButton *chatBtu = [GYButton buttonWithType:GYButtonTypeVerticalImageTitle];
    chatBtu.frame = frame;
    chatBtu.fontSize = widthScale(14, iphone6_W);
    chatBtu.tag = tag;
    [chatBtu setImage:[UIImage imageNamed:imageName] forState:GYControlStateNormal];
    [chatBtu setTitle:title forState:GYControlStateNormal];
    chatBtu.titleTopOrBottonMarginToSuperView = 0;
    [chatBtu setBackgroundColor:[UIColor clearColor] forState:GYControlStateNormal];
    chatBtu.imageSize = CGSizeMake(tag == 1 ? 30 : 20, tag == 1 ? 15 : 20);
    [chatBtu addTarget:self action:@selector(btuClick:) forControlEvents:UIControlEventTouchUpInside];
    [chatBtu setTitleColor:[UIColor whiteColor] forState:GYControlStateNormal];
    
    [self addSubview:chatBtu];
}

#pragma mark -- 懒加载

- (NSArray *)btnTitles {
    if (!_btnTitles) {
        _btnTitles = @[@"钥匙",@"锁具",@"任务",@"日志"];
    }
    
    return _btnTitles;
}

- (NSArray *)btnImages {
    if (!_btnImages) {
        _btnImages = @[@"yaoshi",@"suo",@"renwu",@"rizhi"];
    }
    
    return _btnImages;
}





















@end
